package 界面显示;

import java.sql.*;
import java.util.Scanner;

public class 主界面 {

    public static void main(String[] args) throws SQLException {
        System.out.println("-----欢迎来到图书管理系统-----");
        System.out.println("-----请选择对应的功能-----");
        System.out.println("1.登录 ");
        System.out.println("-------------------------");
        System.out.println("2.注册");
        System.out.println("-------------------------");
        System.out.println("3.退出");
        System.out.println("-------------------------");
        Scanner sc = new Scanner(System.in);
        boolean flag = true;
        while(flag){
            System.out.print("请输入您的选择:");
            int a = sc.nextInt();
            switch (a){
                case 1:
                    login();
                    break;


                case 2:
                    //TODO：待实现
                    System.out.println("=====该版本为内测版本，暂时不支持注册，请直接登录=======");
                    break;
                case 3:
                    System.out.println("------欢迎您的使用---------");
                    System.out.println("------下次再见------------");
                    flag = false;
                    break;
                default:
                    System.out.println("--------请您不要卡bug，快点重新输入-----------");
            }
        }
    }

    public static void login() throws SQLException {
        //首先进行数据库的预连接
        Connection conn = null;
        Statement s = null;
        ResultSet r = null;

        //然后准备从键盘接收输入的账号和密码
        Scanner sc = new Scanner(System.in);
        System.out.println("------------欢迎来到登录界面---------");
        System.out.print("请输入您的账号：");
        try {
            //先通过反射加载一个类，然后数据库驱动包在这种操作下，进行对应的初始化
            Class.forName("com.mysql.jdbc.Driver");
            //获取数据库链接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybookstore?user=root&password=123456&useUnicode=true&characterEncoding=UTF-8&useSSL=false");
            //通过连接对象创造操作对象statement
            s = conn.createStatement();
            int account = sc.nextInt();
            r = s.executeQuery("select * from user where account = " + account);
            if(r.next()){
                String SQLname = r.getString("name");
                System.out.println("-----------欢迎您！" + SQLname + "请输入您的密码---------");
                System.out.print("请在此输入您的密码：");
                int Password = sc.nextInt();
                int SQLPassword = r.getInt("password");
                if(Password == SQLPassword){
                    function();
                }else{
                    System.out.println("----------您输入的密码不正确，我怀疑您不是本人操作，即将返回初始页面重新选择-------");
                }
            }else {
                System.out.println("-------你输入的账号不存在,即将返回初始界面，请您重新选择--------");
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
    //功能界面
    public static void function(){
        //首先进行数据库的预连接
        Connection conn = null;
        Statement s = null;
        //然后准备从键盘进行相关功能的接收
        Scanner sc = new Scanner(System.in);
        try {
            //先通过反射加载一个类，然后数据库驱动包在这种操作下，进行对应的初始化
            Class.forName("com.mysql.jdbc.Driver");
            //获取数据库链接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybookstore?user=root&password=123456&useUnicode=true&characterEncoding=UTF-8&useSSL=false");
            //通过连接对象创造操作对象statement
            s = conn.createStatement();
            while(true) {
                System.out.println("--------欢迎来到功能界面，请选择您的操作！！！-----------");
                System.out.println("1.查看当前图书库存");
                System.out.println("------------------------------");
                System.out.println("2.借阅所需图书");
                System.out.println("------------------------------");
                System.out.println("3.增加新的图书库存");
                System.out.println("------------------------------");
                System.out.println("4.退出");
                System.out.println("------------------------------");
                System.out.println("剩余功能待增加.........");
                System.out.print("请输入您的选择：");
                int key = sc.nextInt();
                switch (key){
                    case 1:
                        function1(s);
                        break;
                    case 2:
                        function2(s);
                        break;
                    case 3:
                        function3(s);
                    case 4:
                        return;
                    default:
                        System.out.println("--------不要乱选，请选择以上的四个选项------------");
                }
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    public static void function1(Statement s) throws SQLException {
        ResultSet r = s.executeQuery("select * from bookstore");
        System.out.println("----------------------------------------------");
        while(r.next()){
            int id = r.getInt("id");
            String name = r.getString("name");
            int number = r.getInt("number");
            String status = r.getString("status");
            String borrower = r.getString("borrower");
            System.out.println("图书id:" + id + ",  \t图书名称:" + name + ",  \t图书库存：" + number + ",  \t图书状态：" + status + ",  \t借阅人:" + borrower);
        }
        System.out.println("------------------------------------------------");
    }

    public static void function2(Statement s) throws SQLException {
        System.out.println("----------------------------------------------");
        System.out.print("请输入您要借阅的图书id：");
        Scanner sc = new Scanner(System.in);
        int key = sc.nextInt();
        ResultSet r = s.executeQuery("select * from bookstore where id = '" + key + "'");
        while(r.next()){
            int id = r.getInt("id");
            String name = r.getString("name");
            int number = r.getInt("number");
            String status = r.getString("status");
            String borrower = r.getString("borrower");
            System.out.println("图书id:" + id + ",  \t图书名称:" + name + ",  \t图书库存：" + number + ",  \t图书状态：" + status + ",  \t借阅人:" + borrower);
            if(!r.getString("status").equals("未被借阅")){
                System.out.println("----------当前图书已被借阅，请进行其他选择功能的选择!----------");
                return;
            }
            System.out.print("当前图书可以被借阅，请输入借阅人的账号:");
            int account = sc.nextInt();
            r = s.executeQuery("select * from user where account = " + account);
            if(r.next()){
                String BorrowerName = r.getString("name");
                s.executeUpdate("update bookstore set status = '被借阅中' where id = " + id);
                s.executeUpdate("update bookstore set borrower = '" + BorrowerName + "'where id = " + id);
                s.executeUpdate("update user set BorrowName = '" + name + "' where account = " + account);
                System.out.println("------当前图书已经借阅成功，请选择您要进行的其他操作----------");
                return;
            }
            System.out.println("-------------当前账号不存在,请确定您的借阅人是否存在--------------");
        }
    }

    public static void function3(Statement s) throws SQLException {
        Scanner sc = new Scanner(System.in);
        System.out.println("----------------------------");
        System.out.print("请输入您要添加的图书名称：");
        String name = sc.next();
        System.out.print("请输入您的存入量：");
        int number = sc.nextInt();
        int r = s.executeUpdate("insert into bookstore(name,number,status,borrower) values ('《"+name + "》'," + number + ",'未被借阅','暂无借阅人');");
        System.out.println("-------------插入成功，当前表的数据如下-------------------");
        function1(s);
    }
}
